package fr.cdj.thebox.client.menu.ui;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.inject.Inject;
import com.google.inject.Provider;

import fr.cdj.thebox.client.resources.TheboxBundle;
import fr.cdj.thebox.shared.FileMetaData.CompareFileType;

public class MenuDisplayImpl extends SimplePanel implements MenuDisplay {

	private MenuItem deleteButton;
	private MenuItem downloadButton;
	private MenuItem logoutButton;
	private SortItem sortItem;
	private SearchItem searchItem;
	
	@Inject
	public MenuDisplayImpl(TheboxBundle resource, SortItem sortItem, SearchItem searchItem, Provider<MenuItem> menuitemProvider) {
		this.setStyleName(resource.getStyle().menu());
		this.sortItem = sortItem;
		this.searchItem = searchItem;
		
		downloadButton = menuitemProvider.get();
		downloadButton.setTitle("Download");
		downloadButton.addButtonStyleName(resource.getStyle().download());
		
		deleteButton = menuitemProvider.get();
		deleteButton.addButtonStyleName(resource.getStyle().delete());
		deleteButton.setTitle("Delete");
		deleteButton.setVisible(false);
		
		logoutButton = menuitemProvider.get();
		logoutButton.setTitle("Logout");
		logoutButton.addButtonStyleName(resource.getStyle().logout());
		
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setSize("100%", "100%");
		hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hPanel.add(searchItem);
		hPanel.add(sortItem);
		hPanel.add(downloadButton);
		hPanel.add(deleteButton);
		hPanel.add(logoutButton);
		hPanel.setCellWidth(logoutButton, "100%");
		hPanel.setCellHorizontalAlignment(logoutButton, HasHorizontalAlignment.ALIGN_RIGHT);
		
		this.setWidget(hPanel);
	}
	
	@Override
	public HasClickHandlers getLogoutClickHandlers() {
		return logoutButton;
	}
	
	@Override
	public HasClickHandlers getDeleteClickHandlers() {
		return deleteButton;
	}
	
	@Override
	public HasClickHandlers getDownloadClickHandlers() {
		return downloadButton;
	}
	
	@Override
	public CompareFileType getSortValue() {
		return sortItem.getValue();
	}
	
	@Override
	public HasClickHandlers getSortClickHandlers() {
		return sortItem.getSortClickHandlers();
	}

	@Override
	public void setShowDelete(boolean canDelete) {
		deleteButton.setVisible(canDelete);
	}
	
	@Override
	public HasValueChangeHandlers<String> getSearchValueChangedHandlers() {
		return searchItem;
	}
	
}
